function cp_man() {
     /**
     * 数组去重
     **/
      Array.prototype.distinct = function () {
        var arr = this, result = [], i, j, len = arr.length;
        for (i = 0; i < len; i++) {
            for (j = i + 1; j < len; j++) {
                if (arr[i] === arr[j]) {
                    j = ++i;
                }
            }
            result.push(arr[i]);
        }
        return result;
    }
    var cx_ary = []
    function AddDate(date, days) {
        if (!days) {
            days = 1;
        }
        var date = new Date(date);
        date.setDate(date.getDate() + days);
        return date;
    }
    function autoWeightKey(record) {
        record.set('WeightKey', parseInt(record.get('CustomerKey')) + parseInt(record.get('SaOrderKey')) + parseInt(record.get('DistanceKey')) + parseInt(record.get('OtherKey')))
    }
    function wnd(list) {
        let fields = [];
        var table = getListWithMultParams('TableProperty', { Name: '=WorkshopIssueResDemandPlan', db_id: 'config' });
        table.sort((a, b) => b.ID - a.ID);
        var cols = getListWithMultParams('columnproperty', { TableProperty: '=' + table.ID, db_id: 'config' });
        cols.forEach(element => {
            fields.push(element.Name)
        });
        var wqStore = Ext.create('Ext.data.Store', {
            fields: fields,
            sortable: true,
            //sorters: [{ property: "ReqDate", direction: "ASC" }], //给定一个默认的排序方式      
            data: list
        });
        var wqGrid = getTop().Ext.create('Ext.grid.Panel', {
            store: wqStore,
            region: 'south',
            frame: false,
            width: 900,
            height: '100%',
            viewConfig: {
                columnLines: true,
                stripeRows: true,
                enableTextSelection: true,
                animateShadow: true,
                forceFit: true,
                getRowClass: function (record) {
                    let nowDate = new Date().format('yyyy/MM/dd');
                    let tmdate = new Date(record.data.DuDate).format('yyyy/MM/dd');;
                    if (tmdate < nowDate) {
                        return "bglightred";
                    }
                }
            },
            selModel: {
                mode: 'MULTI',
                checkboxSelect: true,
                injectCheckbox: 1,
                type: 'checkboxmodel'
            },
            plugins: {
                gridfilters: true
            },
            columns: [
                { header: '', xtype: 'rownumberer', width: 45, align: 'center', sortable: false, menuDisabled: true },
                { text: '编码', tooltip: '编码', dataIndex: 'Number', sortable: false, width: 118, edittype: 'default', menuDisabled: true }
                , { text: '车间', tooltip: '车间\nParentID', dataIndex: 'ParentID', sortable: false, width: 76, edittype: 'default', menuDisabled: true }
                , { text: '设备工作状态', tooltip: '设备工作状态', dataIndex: 'FWorkStatus', sortable: false, width: 100, edittype: 'default', menuDisabled: true }
                , { text: '名称', tooltip: '名称', dataIndex: 'Name', sortable: false, width: 74, edittype: 'default', menuDisabled: true }
                , {
                    text: '类型', tooltip: '类型', dataIndex: 'Type', sortable: false, width: 48, fkstore: {
                        valueName: '_value',
                        displayName: '_display',
                        data: { "1": "车间", "2": "设备", "3": "任务", "4": "工艺阶段" }
                    }, valueField: '_value', displayField: '_display', iscustomer: true, isfk: true, renderer: foreignColumnRenderer, edittype: 'default', menuDisabled: true
                }
                , {
                    text: '产品编码', tooltip: '产品编码', dataIndex: 'InventoryID', sortable: false, width: 172, fkstore: {
                        valueName: '_value',
                        displayName: '_display',
                        data: { "00-3dpgp": "00-3DPGP/3D眼镜偏光片", "1301010051": "1301010051/青蓝B6153-SA" }
                    }, valueField: '_value', displayField: '_display', isfk: true, rktablename: 'Inventory', rkname: 'Inventory_ID', renderer: foreignColumnRenderer, edittype: 'default', menuDisabled: true
                }
                , { text: '计划产量', tooltip: '计划产量', dataIndex: 'PlanQty', sortable: false, width: 74, edittype: 'default', menuDisabled: true }
                , { text: 'T计', tooltip: 'T计', dataIndex: 'PlanDate', sortable: false, width: 55, edittype: 'default', menuDisabled: true }
                , { text: '优先权重', tooltip: '优先权重', dataIndex: 'WeightKey', sortable: false, width: 74, edittype: 'default', menuDisabled: true }
                , {
                    text: '客户级别', tooltip: '客户级别', dataIndex: 'CustomerKey', sortable: false, width: 74, fkstore: {
                        valueName: '_value',
                        displayName: '_display',
                        data: { "4": "VIP", "0": "一般" }
                    }, valueField: '_value', displayField: '_display', iscustomer: true, isfk: true, renderer: foreignColumnRenderer, edittype: 'default', menuDisabled: true
                }
                , {
                    text: '订单优先级别', tooltip: '订单优先级别', dataIndex: 'SaOrderKey', sortable: false, width: 100, fkstore: {
                        valueName: '_value',
                        displayName: '_display',
                        data: { "8": "加急", "0": "一般" }
                    }, valueField: '_value', displayField: '_display', iscustomer: true, isfk: true, renderer: foreignColumnRenderer, edittype: 'default', menuDisabled: true
                }
                , {
                    text: '距离优先级别', tooltip: '距离优先级别', dataIndex: 'DistanceKey', sortable: false, width: 100, fkstore: {
                        valueName: '_value',
                        displayName: '_display',
                        data: { "2": "远", "0": "近" }
                    }, valueField: '_value', displayField: '_display', iscustomer: true, isfk: true, renderer: foreignColumnRenderer, edittype: 'default', menuDisabled: true
                }
                , {
                    text: '其他优先级别', tooltip: '其他优先级别', dataIndex: 'OtherKey', sortable: false, width: 100, fkstore: {
                        valueName: '_value',
                        displayName: '_display',
                        data: { "1": "优先", "0": "一般" }
                    }, valueField: '_value', displayField: '_display', iscustomer: true, isfk: true, renderer: foreignColumnRenderer, edittype: 'default', menuDisabled: true
                }
                , { text: '车间计划', tooltip: '车间计划', dataIndex: 'SelfMfg_OrdersPO', sortable: false, width: 74, edittype: 'default', menuDisabled: true }
                , {
                    text: '设备编码', tooltip: '设备编码', dataIndex: 'Resource_ID', sortable: false, width: 140, fkstore: {
                        valueName: '_value',
                        displayName: '_display',
                        data: { "c1301": "C1301", "c1302": "C1302" }
                    }, valueField: '_value', displayField: '_display', isfk: true, rktablename: 'Resource', rkname: 'Resource_ID', renderer: foreignColumnRenderer, edittype: 'default', menuDisabled: true
                }
                , {
                    text: '是否选入排程', tooltip: '是否选入排程', dataIndex: 'IsProduceResIssuePlan', sortable: false, width: 140, fkstore: {
                        valueName: '_value',
                        displayName: '_display',
                        data: { "是": "2", "否": "0" }
                    }, valueField: '_value', displayField: '_display', iscustomer: true, isfk: true, renderer: foreignColumnRenderer, edittype: 'default', menuDisabled: true
                }
            ],
            tbar: {
                //items: [oneTbar]  
            },
        });
        var init_wind = getTop().Ext.create('Ext.window.Window', {
            title: '未清生产订单',
            width: 830,
            height: 480,
            modal: true,
            maximizable: true,
            items: [wqGrid],
            listeners: {
                resize: function (wn, width, height) {
                    let __grid = wn.items.items[0];
                    __grid.setHeight(height - 80);
                    __grid.setWidth(width - 15);
                }
            },
            fbar: [
                {
                    type: 'button',
                    text: '选择',
                    handler: function () {
                        let selectrows = wqGrid.getSelectionModel().getSelection();
                        if (selectrows.length > 0) {
                            grid.getStore().add(selectrows)
                            init_wind.close();
                            return true;
                        } else {
                            Alert('提示：', '请选择数据！');
                            return false;
                        }
                    }
                }, {
                    type: 'button',
                    text: '取消',
                    handler: function () {
                        init_wind.close();
                    }
                }
            ]
        });
        init_wind.show();
    }
    //自动排序
    function autoSort(resId) {
        let obj = new Object();
        let list = grid.getStore().getData().items;
        if(resId){
            list = grid.getStore().queryRecords('Resource_ID',resId)
        }
        
        list.forEach(function (record) {
            let resourceId = record.get('Resource_ID');

            // let startDate = new Date(record.get('PlanDateStart'))
            // let endDate = new Date(record.get('PlanDateEnd'))
            // var date3 = endDate - startDate;    //时间差的毫秒数
            var hours = parseFloat(record.get('PlanDate'))
            //Math.floor(date3 / (3600 * 1000))

            if (obj[resourceId]) {
                if (Object.keys(obj).find(x => x == record.get('Resource_ID'))) {
                    record.set('ScheduleStartDate', obj[resourceId].format('yyyy-MM-dd HH:mm'))
                    obj[resourceId] = new Date(obj[resourceId].setHours(obj[resourceId].getHours() + hours))
                    record.set('ScheduleEndDate', JSON.stringify(obj[resourceId].format('yyyy-MM-dd HH:mm')).replace(/\"/g, ""))
                }
            } else {
                record.set('ScheduleStartDate', new Date().format('yyyy-MM-dd HH:mm'))
                obj[resourceId] = new Date(new Date().setHours(new Date().getHours() + hours))
                record.set('ScheduleEndDate', JSON.stringify(obj[resourceId].format('yyyy-MM-dd HH:mm')).replace(/\"/g, ""))

            }
        })
        console.log(obj)
    }
    let workShop_comb = Ext.create('Ext.form.ComboBox', {
        fieldLabel: '车间',
        labelWidth: 35,
        width: 175,
        editable: false,
        store: Ext.create('Ext.data.Store', {
            fields: ['abbr', 'name'],
            data: []
        }),
        queryMode: 'local',
        displayField: 'name',
        valueField: 'abbr',
        listeners: {
            select: function (combo) {
                var c1 = combo.getValue();
                var chanxianList = getListWithMultParams("WorkShopLine", { Parent_ID: c1 });
                resource_comb.getStore().removeAll();
                resource_comb.getStore().add({ "abbr": "", "name": "全部" })
                chanxianList.forEach(element => {
                    resource_comb.getStore().add({ "abbr": element.Code_ID, "name": element.Description })
                });
            }
        }
    });
    let resource_comb = Ext.create('Ext.form.ComboBox', {
        fieldLabel: '设备',
        labelWidth: 35,
        width: 175,
        editable: false,
        store: Ext.create('Ext.data.Store', {
            fields: ['abbr', 'name'],
            data: []
        }),
        queryMode: 'local',
        displayField: 'name',
        valueField: 'abbr'
    });
    let pc_days = Ext.create('Ext.form.NumberField', {
        xtype: 'numberfield',
        fieldLabel: '天数',
        value: 1,
        mouseWheelEnabled: false,
        labelWidth: 35,
        width: 100,
        maxValue: 999,
        minValue: 1,
        listeners: {
            change: function (opt, newValue, oldValue, eOpts) {
                let start = startDate.getValue();
                if (newValue >= 1) {
                    endDate.setValue(AddDate(start, newValue));
                }
            }
        }
    })
    let startDate = Ext.create('Ext.form.DateField', {
        fieldLabel: '开始日期',
        labelWidth: 65,
        width: 175,
        format: 'Y/m/d',
        value: new Date(),
        listeners: {
            change: function (opt, newValue, oldValue, eOpts) {
                let start = startDate.getValue();
                if (pc_days.getValue() >= 1) {
                    endDate.setValue(AddDate(start, pc_days.getValue()));
                }
            }
        }
    })
    let endDate = Ext.create('Ext.form.DateField', {
        fieldLabel: '截止日期',
        labelWidth: 65,
        width: 175,
        format: 'Y/m/d',
        value: new Date(),
        listeners: {
            change: function (opt, newValue, oldValue, eOpts) {
                let start = startDate.getValue();
                if (pc_days.getValue() >= 1) {
                    endDate.setValue(AddDate(start, pc_days.getValue()));
                }
            }
        }
    })


    let cols = ['Number', 'Name', 'Type', 'InventoryID', 'Description', 'PlanDateStart2', 'PlanDate', 'PlanDateStart', 'ScheduleStartDate', 'ScheduleEndDate', 'Matching',
        'PlanDateEnd', 'OrderKey', 'WeightKey', 'CustomerKey', 'SaOrderKey', 'DistanceKey', 'OtherKey', 'SelfMfg_OrdersPO', 'Resource_ID', 'IsProduceResIssuePlan']
    var store = Ext.create('Ext.data.Store', {
        fields: cols,
        data: [],
        sorters: [{ property: "WeightKey", direction: "DESC" }], //给定一个默认的排序方式      
        groupField: 'Resource_ID',
    });
    var searTbar = new Ext.Toolbar({
        dock: 'top',
        items: [
            workShop_comb, pc_days, startDate, endDate, resource_comb, {
                text: '查询',
                glyph: 0xf002,
                handler: function () {
                    
                    var cond = {
                        t: "and", child: [{ c: "Type", cp: '=', v: '3' }]
                    }
                    if (workShop_comb.getValue()) {
                        cond.child.push({ c: "ParentID", cp: '=', v: workShop_comb.getValue() })
                    }
                    if (resource_comb.getValue()) {
                        cond.child.push({ c: "Resource_ID", cp: '=', v: resource_comb.getValue() })
                    }
                    if (startDate.getValue()) {
                        cond.child.push({ c: "PlanDateStart", cp: '>=', v: startDate.getValue() })
                    }
                    if (endDate.getValue()) {
                        cond.child.push({ c: "PlanDateStart", cp: '<=', v: endDate.getValue() })
                    }
                    Wait.Start(null, "正在执行查询，请稍候.....");
                    getListWithMultParams("WorkshopIssueResDemandPlan", { _cdt: jsonEncode(cond), __all: true }, null, {
                        callback: function (vitems) {
                            Wait.hide();
                            //store.removeAll();
                            vitems.forEach(element => {
                                if (store.find("Number", element.Number) == -1) {
                                    element.ScheduleStartDate = '';
                                    element.ScheduleEndDate = '';
                                    element.CustomerKey = 0;
                                    element.SaOrderKey = 0;
                                    element.DistanceKey = 0;
                                    element.OtherKey = 0;
                                    element.Matching = 0;
                                    store.add(element)
                                }
                            });
                            store.each(function (record) {
                                if (!vitems.find(x => x.Number == record.get('Number'))) {
                                    store.remove(record)
                                }
                            })
                        }, async: function () {

                        }
                    });
                }
            }
        ]

    });
    var toolTbar = new Ext.Toolbar({
        dock: 'top',
        items: [
            {
                text: '齐套分析',
                glyph: 0xf058,
                handler: function () {
                    Wait.Start(null, "正在执行分析，请稍候.....");
                    Ext.Ajax.request({
                        url: "http://1.116.253.250:44326/api/mingruan/sCItemListLlh",
                        method: "GET",
                        timeout: 10000,
                        success: function (response, options) {
                            let { result } = JSON.parse(response.responseText)
                            if (result && result.length > 0) {
                                grid.getStore().each(function (record) {
                                    let GCDNO = record.get('Number');
                                    let list = getListWithMultParams('Material_Issue_Plan_Detail_CP', { NO: GCDNO });
                                    foreach(list, function (item) {
                                        let result_item = result.find(x => x.fNumber == item.Inventory_ID && x.fxqQty < 0);
                                        if (!result_item) {
                                            record.set('Matching', 1)
                                        }
                                    })
                                })
                            }
                            Wait.hide();
                        },
                        failure: function (response, options) {
                            Wait.hide();
                            let result = JSON.parse(response.responseText)
                            alert(result + "  " + options);
                        }
                    });

                }
            },
            {
                text: '查看指定工单缺料',
                glyph: 0xf689,
                handler: function () {
                    let records = grid.getSelection();
                    if (records.length == 1) {
                        let GCDNO = records[0].get('Number');
                        if (!GCDNO) {
                            Alert('错误：', "工单值为空！请检查数据")
                            return false;
                        }
                        Wait.Start(null, "正在执行查询，请稍候.....");
                        Ext.Ajax.request({
                            url: "http://1.116.253.250:44326/api/mingruan/sCItemListLlh",
                            method: "GET",
                            timeout: 10000,
                            success: function (response, options) {
                                let { result } = JSON.parse(response.responseText)
                                Wait.hide();
                                if (result && result.length > 0) {
                                    console.log(result.filter((item) => {
                                        return item.fxqQty < 0
                                    }))
                                    let list = getListWithMultParams('Material_Issue_Plan_Detail_CP', { NO: GCDNO });
                                    let qlAry = []
                                    foreach(list, function (item) {
                                        let result_item = result.find(x => x.fNumber == item.Inventory_ID && x.fxqQty < 0);
                                        if (result_item) {
                                            qlAry.push({
                                                AUFNR: GCDNO,
                                                DATE: item.Date,
                                                MATNR: result_item.Inventory_ID,
                                                MAKTX: result_item.fName,
                                                BDMNG: item.Require_Qty,
                                                QL_QTY: result_item.fxqQty,
                                                MEINS: result_item.fModel
                                            })
                                        }
                                    })
                                    let qlGrid = Ext.create('Ext.grid.Panel', {
                                        store: Ext.create('Ext.data.Store', {
                                            fields: ['AUFNR', 'DATE', 'MATNR', 'MAKTX', 'BDMNG', 'QL_QTY', 'MEINS'],
                                            data: qlAry
                                        }),
                                        height: 400,
                                        stripeRows: true,
                                        viewConfig: { forceFit: true },
                                        columns: [
                                            { header: '', xtype: 'rownumberer', width: 45, align: 'center', sortable: false, menuDisabled: true },
                                            { header: '工程单号', dataIndex: 'AUFNR', width: 117, align: 'center', sortable: false, menuDisabled: true },
                                            { header: '生产单日期', dataIndex: 'DATE', width: 117, align: 'center', sortable: false, menuDisabled: true },
                                            { header: '物料编码', dataIndex: 'MATNR', width: 100, align: 'center', sortable: false, menuDisabled: true },
                                            { header: '物料名称', dataIndex: 'MAKTX', width: 330, align: 'center', sortable: false, menuDisabled: true },
                                            { header: '需求数量', dataIndex: 'BDMNG', width: 73, align: 'center', sortable: false, menuDisabled: true },
                                            { header: '缺料数量', dataIndex: 'QL_QTY', width: 73, align: 'center', sortable: false, menuDisabled: true },
                                            { header: '单位', dataIndex: 'MEINS', width: 47, align: 'center', sortable: false, menuDisabled: true }
                                        ]
                                    });
                                    var init_wind = Ext.create('Ext.window.Window', {
                                        title: GCDNO + ':短缺物料',
                                        width: 830,
                                        height: 480,
                                        modal: true,
                                        items: [qlGrid],
                                        listeners: {
                                            resize: function (wn, width, height, eOpts) {
                                                let qlGrid = wn.items.items[0];
                                                qlGrid.setHeight(height - 80);
                                                qlGrid.setWidth(width - 15);
                                            }
                                        },
                                        fbar: [{
                                            type: 'button',
                                            text: '确定',
                                            handler: function () { init_wind.close(); }
                                        }]
                                    });
                                    init_wind.show();
                                    console.log(qlAry)
                                }
                            },
                            failure: function (response, options) {
                                Wait.hide();
                                let result = JSON.parse(response.responseText)
                                alert(result + "  " + options);
                            }
                        });
                    } else {
                        Alert('提示：', "请选择需要查询的工单！")
                        return false;
                    }
                }
            },
            {
                text: '未清工单',
                glyph: 0xf4ce,
                handler: function () {
                    var cond = {
                        t: "and", child: [
                            { c: "Type", cp: '==', v: '3' }
                        ]
                    }
                    let gd_arry = [];
                    grid.getStore().each(function (record) {
                        gd_arry.push(record.get('Number'))
                    })
                    if (gd_arry.length > 0) {
                        cond.child.push({ c: "Number", cp: 'notin', v: gd_arry.join(';') })
                    }
                    getListWithMultParams("WorkshopIssueResDemandPlan", { _cdt: jsonEncode(cond), __all: true }, null, {
                        callback: function (vitems) {
                            wnd(vitems)

                        }, async: function () {

                        }
                    });
                    //var url = "/WorkshopIssueResDemandPlan_ViewCJ?disble_operate_type=Add,Edit,Delete,Search&selectmode=true&__all=true&_cdt=" + encodeURI(jsonEncode(cond, false));
                    //console.log(url)
                    // parent.openWindow("浏览工程单", url, 900, 600, function (v) {
                    //     if (v != null && typeof (v) == "object") {
                    //         if (v.length > 1) {
                    //             if (v[0].success == 'true' || v[0].success == true) {
                    //                 let ary = []
                    //                 foreach(v, function (vitem, i) {
                    //                     if (i > 0) {
                    //                         ary.push(vitem)
                    //                     }
                    //                 });
                    //                 grid.getStore().add(ary)
                    //             }
                    //         }
                    //     }
                    // }, "", null, null, isjsschema);
                }
            },
            {
                text: '自动排序',
                glyph: 0xf25b,
                handler: function () {
                    let records = grid.getSelection();
                    if(records.length > 0){
                        autoSort(records[0].get('Resource_ID'))
                    }else{
                        autoSort()
                    }
                    
                }
            }, {
                text: '删除',
                glyph: 0xf2ed,
                handler: function () {

                    let records = grid.getSelection();
                    if (records.length > 0) {
                        Confirm('提示：', "确定删除？", function () {
                            grid.getStore().remove(records[0])
                        }, null, null)
                    }
                }
            }, {
                text: '可视化排程',
                glyph: 0xf51c,
                handler: function () {
                    let project = guid();
                    let Resources_Ary = [];
                    let updatelist = new Array();
                    let table = '_ResourceGantt';
                    var list0 = getListWithMultParams("_ResourceGantt", { Maker: getTopWindow().username });
                    foreach(list0, function (vitem) {
                        updatelist.push(new UpdateItemObject(table, { UID: vitem.UID }, Action.Delete));
                    });
                    Batch.updateDatabase(updatelist, false, function (vback) {
                        var vitem = vback[0];
                        if (vitem.success === false) {
                            Alert("", vitem.message);
                        } else {
                            grid.getStore().each(function (record) {
                                if (!Resources_Ary.find(x => x.abbr == record.get('Resource_ID'))) {
                                    Resources_Ary.push(cx_ary.find(x => x.abbr == record.get('Resource_ID')))
                                }
                                updatelist.push(new UpdateItemObject(table, {
                                    UID: record.get('Number'),
                                    Name: record.get('Number'),
                                    Inventory_ID: record.get('InventoryID'),
                                    Type: 10,
                                    ProjectNo: project,
                                    ParentID: record.get('Resource_ID'),
                                    Start: record.get('ScheduleStartDate').format('yyyy-MM-dd HH:mm'),
                                    Finish: record.get('ScheduleEndDate').format('yyyy-MM-dd HH:mm'),
                                    PercentComplete: 0,
                                    Duration: parseFloat(record.get('PlanDate')) * 3600,
                                    Maker: getTopWindow().username
                                }, Action.Add));
                            })
                            Resources_Ary.forEach(element => {
                                updatelist.push(new UpdateItemObject(table, {
                                    UID: element.abbr,
                                    ProjectNo: project,
                                    Name: element.name,
                                    Type: 5,
                                    Maker: getTopWindow().username
                                }, Action.Add));
                            });
                            Batch.updateDatabase(updatelist, false, function (vback) {
                                var vitem = vback[0];
                                if (vitem.success === false) {
                                    Alert("", vitem.message);
                                } else {
                                    getTopWindow().openWindow("工程单排产", "/_ResourceGantt?disble_operate_type=export&ProjectNo=" + project, 900, 600, function () { }, "", null, null, isjsschema);
                                }
                            });
                        }
                    });

                }
            }, {
                text: '虚拟工单',
                glyph: 0xf518,
                handler: function () {
                    let gd_arry = [];
                    var cond = {
                        t: "and", child: []
                    }
                    grid.getStore().each(function (record) {
                        gd_arry.push(record.get('Number'))
                    })
                    gd_arry = gd_arry.distinct();
                    let gcd = gd_arry.join(';');
                    let con_ary = [
                        { c: "EvidenceNumber", cp: 'notin', v: gcd },
                        { c: "Verify", cp: '=', v: 1 }
                    ];
                    cond.child = con_ary;
                    function wnd(list) {
                        foreach(list, function (el) {
                            var work = cx_ary.filter(function (p) {
                                if (p.abbr == el.Factory) {
                                    return p;
                                }
                            });
                            var line = cx_ary.filter(function (p) {
                                if (p.abbr == el.ProductionLine) {
                                    return p;
                                }
                            });
                            el.Factory_name = work.length == 0 ? "" : work[0].name;
                            el.ProductionLine_name = line.length == 0 ? "" : line[0].name;
                        });
                        var xnStore = Ext.create('Ext.data.Store', {
                            fields: ['EvidenceNumber', 'Factory', 'Factory_name', 'ProductionLine', 'ProductionLine_name', 'BeginDate', 'EndDate', 'Maker', 'SumWrkHour', 'SumCapacity', 'Reason', 'Summary'],
                            data: list
                        });
                        var xnGrid = getTop().Ext.create('Ext.grid.Panel', {
                            store: xnStore,
                            region: 'south',
                            frame: false,
                            width: 900,
                            height: '100%',
                            viewConfig: {
                                columnLines: true,
                                stripeRows: true,
                                enableTextSelection: true,
                                animateShadow: true,
                                forceFit: true
                            },
                            selModel: {
                                mode: 'Single',
                                checkboxSelect: true,
                                injectCheckbox: 1,
                                type: 'checkboxmodel'
                            },
                            columns: [
                                { header: '', xtype: 'rownumberer', width: 45, align: 'center', sortable: false, menuDisabled: true },
                                {
                                    dataIndex: 'EvidenceNumber', header: '工单号', width: 120, sortable: false, menuDisabled: true
                                },
                                { dataIndex: 'Factory', header: '工厂', hidden: true, width: 109, sortable: false, menuDisabled: true },
                                { dataIndex: 'Factory_name', header: '工厂', width: 109, sortable: false, menuDisabled: true },
                                {
                                    dataIndex: 'ProductionLine', header: '产线', hidden: true, width: 87, sortable: false, menuDisabled: true
                                },
                                { dataIndex: 'ProductionLine_name', header: '产线', width: 109, sortable: false, menuDisabled: true },
                                { dataIndex: 'BeginDate', header: '开始日期', width: 112, sortable: false, menuDisabled: true },
                                { dataIndex: 'EndDate', header: '截止日期', width: 112, sortable: false, menuDisabled: true },
                                { dataIndex: 'SumWrkHour', header: '总虚拟工时', width: 74, sortable: false, menuDisabled: true },
                                { dataIndex: 'SumCapacity', header: '总虚拟产能', width: 61, hidden: true, sortable: false, menuDisabled: true },
                                { dataIndex: 'Reason', header: '原因', width: 100, sortable: false, menuDisabled: true },
                                { dataIndex: 'Summary', header: '备注', width: 74, sortable: false, menuDisabled: true },
                                { dataIndex: 'Maker', header: '制单人', width: 200, sortable: false, menuDisabled: true },
                            ]
                        });
                        var init_wind = getTop().Ext.create('Ext.window.Window', {
                            title: '虚拟工程单',
                            width: 830,
                            height: 480,
                            modal: true,
                            maximizable: true,
                            items: [xnGrid],
                            listeners: {
                                resize: function (wn, width, height, eOpts) {
                                    let xnGrid = wn.items.items[0];
                                    xnGrid.setHeight(height - 80);
                                    xnGrid.setWidth(width - 15);
                                }
                            },
                            fbar: [
                                {
                                    type: 'button',
                                    text: '选择',
                                    handler: function () {
                                        let selectrows = xnGrid.getSelectionModel().getSelection();
                                        if (selectrows.length > 0) {
                                            let selectrow = selectrows[0].data;
                                            grid.getStore().add({
                                                Number: selectrow.EvidenceNumber,
                                                Name: "虚拟工单",
                                                Resource_ID: selectrow.ProductionLine,
                                                PlanDate: selectrow.SumWrkHour,
                                                PlanDateStart: selectrow.BeginDate,
                                                PlanDateEnd: selectrow.EndDate,
                                                Description:selectrow.Reason,
                                                CustomerKey : 0,
                                                SaOrderKey : 0,
                                                DistanceKey : 0,
                                                OtherKey : 0,
                                                Matching : 0,
                                                PlanQty:0
                                            })
                                            init_wind.close();
                                            return true;
                                        } else {
                                            Alert('提示：', '请选择数据！');
                                            return false;
                                        }
                                    }
                                }, {
                                    type: 'button',
                                    text: '取消',
                                    handler: function () {
                                        init_wind.close();
                                    }
                                }
                            ]
                        });
                        init_wind.show();
                    }
                    Wait.Start(null, "正在执行操作，请稍候.....");
                    getListWithMultParams("TblVirtualTicket", { _cdt: jsonEncode(cond) },
                        null, {
                        callback: function (vitems) {
                            Wait.hide();
                            wnd(vitems);
                        },
                        async: function () {

                        }
                    });
                }
            }, '->',
            {
                text: '导出Excel',
                glyph: 0xf1c3,
                handler: function () {
                    grid2Excel(grid, "工单排程任务清单");
                }
            }
        ]

    });
    var grid = getTopWindow().Ext.create('Ext.grid.Panel', {
        store: store,
        stripeRows: true,
        //enableDragDrop: true,
        viewConfig: {
            plugins: {
                ptype: 'gridviewdragdrop',
                //dragGroup :'DragDropGroup',
                enableDrag: true,
                enableDrop: true
            },
            listeners: {
                drop: function (node, data, overModel, dropPosition, eOpts) {
                    console.log(data)
                    autoSort(data.event.record.get('Resource_ID'));
                }
            },
            forceFit: true,
        },
        features: [
            {
                groupHeaderTpl: [
                    '设备: {name:this.formatName}',
                    {
                        formatName: function (name, opt) {
                            return cx_ary.find(x => x.abbr == name).name;
                        }
                    }
                ],
                ftype: 'groupingsummary'
            }
        ],
        plugins: {
            ptype: 'cellediting',
            clicksToEdit: 1,
            gridfilters: true,
        },
        columns: [
            { header: '', xtype: 'rownumberer', width: 45, align: 'center', sortable: false, menuDisabled: true },
            { header: '编码', dataIndex: 'Number', width: 117, align: 'center', sortable: false, menuDisabled: true },
            { header: '名称', dataIndex: 'Name', width: 117, align: 'center', hidden: true, sortable: false, menuDisabled: true },
            { header: '类型', dataIndex: 'Type', width: 100, align: 'center', sortable: false, menuDisabled: true, hidden: true },
            { header: '物料编码', dataIndex: 'InventoryID', width: 200, align: 'center', sortable: false, menuDisabled: true },
            { header: '物料名称', dataIndex: 'Description', width: 200, align: 'center', sortable: false, menuDisabled: true },
            {
                header: '齐', dataIndex: 'Matching', width: 40, align: 'center', sortable: false, menuDisabled: true, renderer: function (value) {
                    let color = 'crimson'
                    if (value == 1) {
                        color = 'green'
                    }
                    return `<div style="background-color:${color};height: 15px;width: 15px;border-radius: 50%;margin:0 auto"></div>`
                }
            },
            { header: '设备编码', dataIndex: 'Resource_ID', width: 110, align: 'center', hidden: true, sortable: false, menuDisabled: true },
            { header: '计划产量', dataIndex: 'PlanQty', width: 90, summaryType: 'sum', align: 'center', sortable: false, menuDisabled: true },
            { header: 't排开', dataIndex: 'ScheduleStartDate', width: 140, align: 'center', sortable: false, menuDisabled: true },
            { header: 't排完', dataIndex: 'ScheduleEndDate', width: 140, align: 'center', sortable: false, menuDisabled: true },
            { header: '计划开工日', dataIndex: 'PlanDateStart2', width: 200, align: 'center', hidden: true, sortable: false, menuDisabled: true },
            { header: 'T计', dataIndex: 'PlanDate', width: 80, summaryType: 'sum', align: 'center', sortable: false, menuDisabled: true },
            { header: 't计开', dataIndex: 'PlanDateStart', xtype: 'datecolumn', format: 'Y/m/d', width: 140, align: 'center', sortable: false, menuDisabled: true },
            { header: 't计完', dataIndex: 'PlanDateEnd', xtype: 'datecolumn', format: 'Y/m/d', width: 140, align: 'center', sortable: false, menuDisabled: true },
            { header: '优先顺序', dataIndex: 'OrderKey', width: 100, align: 'center', sortable: false, menuDisabled: true },
            { header: '优先权重', dataIndex: 'WeightKey', width: 100, align: 'center', sortable: false, menuDisabled: true },
            {
                header: '客户级别', dataIndex: 'CustomerKey', width: 73, align: 'center', sortable: true, menuDisabled: true, editor: {
                    completeOnEnter: false,
                    field: {
                        xtype: 'combobox',
                        store: Ext.create('Ext.data.Store', {
                            fields: ['abbr', 'name'],
                            data: [
                                { "abbr": "4", "name": "VIP" },
                                { "abbr": "0", "name": "一般" }
                            ]
                        }),
                        queryMode: 'local',
                        displayField: 'name',
                        valueField: 'abbr',
                    }
                }, renderer: function (value) {
                    let str = ""
                    if (value == 4) {
                        str = "VIP"
                    } if (value == 0) {
                        str = "一般"
                    }
                    return str;
                }
            },
            {
                header: '订单优先级别', dataIndex: 'SaOrderKey', width: 110, align: 'center', sortable: false, menuDisabled: true, editor: {
                    completeOnEnter: false,
                    field: {
                        xtype: 'combobox',
                        store: Ext.create('Ext.data.Store', {
                            fields: ['abbr', 'name'],
                            data: [
                                { "abbr": "8", "name": "加急" },
                                { "abbr": "0", "name": "一般" }
                            ]
                        }),
                        queryMode: 'local',
                        displayField: 'name',
                        valueField: 'abbr',
                    }
                }, renderer: function (value) {
                    let str = ""
                    if (value == 8) {
                        str = "加急"
                    } if (value == 0) {
                        str = "一般"
                    }
                    return str;
                }
            },
            {
                header: '距离优先级别', dataIndex: 'DistanceKey', width: 110, align: 'center', sortable: false, menuDisabled: true, editor: {
                    completeOnEnter: false,
                    field: {
                        xtype: 'combobox',
                        store: Ext.create('Ext.data.Store', {
                            fields: ['abbr', 'name'],
                            data: [
                                { "abbr": "2", "name": "远" },
                                { "abbr": "0", "name": "近" }
                            ]
                        }),
                        queryMode: 'local',
                        displayField: 'name',
                        valueField: 'abbr',
                    }
                }, renderer: function (value) {
                    let str = ""
                    if (value == 2) {
                        str = "远"
                    } if (value == 0) {
                        str = "近"
                    }
                    return str;
                }
            },
            {
                header: '其他优先级别', dataIndex: 'OtherKey', width: 117, align: 'center', sortable: false, menuDisabled: true, editor: {
                    completeOnEnter: false,
                    field: {
                        xtype: 'combobox',
                        store: Ext.create('Ext.data.Store', {
                            fields: ['abbr', 'name'],
                            data: [
                                { "abbr": "1", "name": "优先" },
                                { "abbr": "0", "name": "一般" }
                            ]
                        }),
                        queryMode: 'local',
                        displayField: 'name',
                        valueField: 'abbr',
                    }
                }, renderer: function (value) {
                    let str = ""
                    if (value == 1) {
                        str = "优先"
                    } if (value == 0) {
                        str = "一般"
                    }
                    return str;
                }
            },
            { header: '车间计划', dataIndex: 'SelfMfg_OrdersPO', width: 117, align: 'center', sortable: false, menuDisabled: true },
            { header: '是否选入排程', dataIndex: 'IsProduceResIssuePlan', width: 330, hidden: true, align: 'center', sortable: false, menuDisabled: true }
        ],
        dockedItems: [toolTbar, searTbar],
        listeners: {
            "afterrender": function (that) {
                console.log(grid.tbar)
                //debugger
                //oneTbar.render(this.config.tbar)
                var workShopList = getListWithMultParams("WorkShopLine", {});
                workShop_comb.getStore().add({ "abbr": "", "name": "全部" })
                workShopList.forEach(element => {
                    if (element.Type == 2) {
                        workShop_comb.getStore().add({ "abbr": element.Code_ID, "name": element.Description })
                    }
                    cx_ary.push({ "abbr": element.Code_ID, "name": element.Description })
                });

                let start = startDate.getValue();
                if (pc_days.getValue() >= 1) {
                    endDate.setValue(AddDate(start, pc_days.getValue()));
                }

            },
            edit: function (editor, context, eOpts) {
                console.log(editor)
                console.log(context)
                autoWeightKey(context.record);
                console.log(eOpts)

            }
        }
    });

    return grid;
}
